<?php
/**
 * Created by PhpStorm.
 * User: medivh
 * Date: 15-6-25
 * Time: 下午2:43
 */
function GetWeek($p = 0){
    $week = array(
        "日","一","二","三","四","五","六"
    );
    $key = date('w',time());
    $keys = ($key+$p) > 6 ? "0" : $key+$p;
    return $week[$keys];
}
function timeFormat($timeInt,$format='Y-m-d H:i:s'){
    if(empty($timeInt)||!is_numeric($timeInt)||!$timeInt){
        return '';
    }
    $d=time()-$timeInt;
    if($d<0){
        return '';
    }else{
        if($d<60){
            return $d.'秒前';
        }else{
            if($d<3600){
                return floor($d/60).'分钟前';
            }else{
                if($d<86400){
                    return floor($d/3600).'小时前';
                }else{
                    if($d<25920000){//30天内
                        return floor($d/86400).'天前';
                    }else{
                        return date($format,$timeInt);
                    }
                }
            }
        }
    }
}
/**
 * @param $str要分割的标签字符串
 * @param $f制定分隔符，默认为%
 */
function label_arr($str,$f='%'){
    $arr = explode($f,$str);
    return $arr;
}
function label($label){
    $result = '';
    foreach(label_arr($label) as $key => $val){
        $result .= "<span class='label label-default ".RandForLabel()."' contenteditable='false'>".$val."</span> ";
    }
    return $result;
}

/**
 * @param $label标签样式
 */
function RandForLabel(){
    $label = array(
        '1' => 'label-info',
        '2'=> 'label-danger',
        '3' => 'label-success',
        '4' => 'label-warning',
        '5' => 'label-primary'
    );
    $a = array_rand($label);
    return $label[$a];
}
function RandForProgress(){
    $label = array(
        '1' => 'bar-info',
        '2'=> 'bar-danger',
        '3' => 'bar-success',
        '4' => 'bar-warning',
        '5' => 'bar-primary'
    );
    $a = array_rand($label);
    return $label[$a];
}
function RandTopBorder(){
    $border = array(
        'red',     'blue',    'green',   'yellow',  'black',
        '#FFF68F', '#FFC1C1', '#FF7F00', '#FF8C69', '#FF4500',
        '#FF00FF', '#EE9A49', '#EE6A50', '#D15FEE', '#CD919E',
        '#BCEE68', '#B0E2FF', '#AB82FF', '#A020F0', '#8DEEEE',
        '#548B54', '#545454', '#53868B', '#528B8B', '#525252',
        '#515151', '#4F94CD', '#4F4F4F', '#4EEE94', '#4D4D4D',
        '#4B0082', '#4A708B', '#4A4A4A', '#48D1CC', '#4876FF',
        '#483D8B', '#474747', '#473C8B', '#4682B4', '#458B74',
        '#458B00', '#454545', '#43CD80', '#436EEE', '#424242',
        '#4169E1', '#40E0D0', '#404040', '#3D3D3D', '#3CB371',
        '#3B3B3B', '#3A5FCD', '#388E8E', '#383838', '#36648B',
        '#363636', '#333333', '#32CD32', '#303030', '#2F4F4F',
        '#2E8B57', '#2E2E2E', '#2B2B2B', '#292929', '#282828',
        '#27408B', '#262626', '#242424', '#228B22', '#218868',
        '#212121', '#20B2AA', '#1F1F1F', '#1E90FF', '#1E1E1E',
        '#1C86EE', '#1C1C1C', '#1A1A1A', '#191970', '#1874CD',
        '#171717', '#141414', '#121212', '#104E8B', '#0F0F0F',
        '#0D0D0D', '#0A0A0A', '#080808', '#050505', '#030303',
        '#00FFFF', '#00FF7F', '#00FF00', '#00FA9A', '#00F5FF',
        '#00EEEE', '#00EE76', '#00EE00', '#00E5EE', '#00CED1',
        '#00CDCD', '#00CD66', '#00CD00'

    );
    $a = array_rand($border);
    return $border[$a];
}
function strCut($str,$length){
    $str = trim($str);
    $string = "";
    if(strlen($str) > $length){
        for($i = 0 ; $i<$length ; $i++){
            if(ord($str) > 127){
                $string .= $str[$i] . $str[$i+1] . $str[$i+2];
                $i = $i + 2;
            }else{
                $string .= $str[$i];
            }
        }
        $string .= "...";
        return $string;
    }
    return $str;
}